package com.ulook.planetmanager.ui.notifications;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ulook.planetmanager.base.BaseViewModel2;
import com.ulook.planetmanager.common.AppServer;
import com.ulook.planetmanager.model.TCommunity;
import com.ulook.planetmanager.model.TTopic;
import com.ulook.planetmanager.model.TUser;
import com.ulook.planetmanager.util.StringUtil;
import com.ulook.planetmanager.util.json.JacksonUtil;
import com.ulook.planetmanager.util.net.AsynRequestUtil;
import com.ulook.planetmanager.util.net.OkHttpException;
import com.ulook.planetmanager.util.net.RequestParams;
import com.ulook.planetmanager.util.net.ResponseCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class NotificationsViewModel extends BaseViewModel2 {

    private File imageFile;
    private String comId;
    private MutableLiveData<List<TCommunity>> communities = new MutableLiveData<>();

    public LiveData<List<TCommunity>> getCommunities(){
        return communities;
    }

    public File getImageFile() {
        return imageFile;
    }

    public void setImageFile(File imageFile) {
        this.imageFile = imageFile;
    }

    public String getComId() {
        return comId;
    }

    public void setComId(String comId) {
        this.comId = comId;
    }

    /**
     * 获取话题分组
     */
    public void getComList(){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TCommunity> tCommunityList = JacksonUtil.json2list((String)res, TCommunity.class);
                    communities.setValue(tCommunityList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("查询分组失败");
            }
        };
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/manage/getComList", null, null, callback);
    }

    /**
     * 添加话题
     */
    public void addTopic(String name){
        if(StringUtil.isEmpty(name)){
            setToastText("话题名不能为空");
            return;
        }
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                setToastText("添加成功");
                imageFile.delete();
                imageFile = null;
                comId = null;
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText(failuer.getEmsg());
            }
        };
        RequestParams params = new RequestParams();
        params.put("name", "#"+name);
        if(comId != null){
            params.put("comuid", comId);
        }
        List<File> images = new ArrayList<>();
        if(imageFile != null){
            images.add(imageFile);
        }

        AsynRequestUtil.postMultipart(AppServer.ROOT_PATH + "/manage/addTopic", params, images, null, callback);

    }

    /**
     * 删除话题
     */
    public void deleteTopic(String name){
        if(StringUtil.isEmpty(name)){
            setToastText("话题名不能为空");
            return;
        }
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                setToastText("删除成功");
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("删除失败");
            }
        };
        RequestParams params = new RequestParams();
        params.put("name", "#"+name);
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/manage/deleteTopic", params, null, callback);
    }
}